//
//  HKNoticeCell.m
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/11.
//

#import "HKNoticeCell.h"
#import "HKNoticeModel.h"
#import <WZLBadge/WZLBadgeImport.h>

static const kKSTopMargin = 16.f;

@interface HKNoticeCell ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *subTitleLab;
@property (nonatomic, strong) UIImageView *arrowImageView;

@end

@implementation HKNoticeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = self.backgroundColor = UIColorFromRGB(0xf7f7f7);
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews {
    [self.contentView addSubview:self.containerView];
    [self.containerView addSubview:self.headerImageView];
    [self.containerView addSubview:self.titleLab];
    [self.containerView addSubview:self.subTitleLab];
    [self.containerView addSubview:self.arrowImageView];
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kKSTopMargin);
        make.left.mas_equalTo(KSLeftMargin);
        make.right.mas_equalTo(self.contentView).offset(-KSRightMargin);
        make.height.mas_equalTo(self.contentView).offset(-kKSTopMargin);
    }];
    
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.size.mas_equalTo(CGSizeMake(48, 48));
        make.centerY.mas_equalTo(self.containerView);
    }];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerImageView.mas_right).offset(1);
        make.top.mas_equalTo(24);
        make.height.mas_equalTo(24.f);
        make.right.mas_equalTo(self.containerView.mas_right).offset(5);
    }];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.titleLab);
        make.height.mas_equalTo(24);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(2);
    }];
    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.containerView);
        make.width.height.equalTo(@16);
        make.right.mas_equalTo(@-10);
    }];
}

- (void)updateContent:(id)content {
    HKNoticeModel *model = KSSAFE_CAST(content, HKNoticeModel);
    [self.headerImageView setImage:[UIImage imageNamed:[model showImageName]]];
    self.titleLab.text = model.title;
    self.subTitleLab.text = model.content;
    if (!model.isRead){
        [self showNewBage];
    }else{
        [self hiddenBage];
    }
}

+ (CGFloat)cellHeightWithContent:(id)content {
    return 114;
}

- (void)showNewBage{
    self.arrowImageView.badgeCenterOffset = CGPointMake(-30, 8);
    [self.arrowImageView showBadgeWithStyle:WBadgeStyleRedDot value:0 animationType:WBadgeAnimTypeNone];
}

- (void)hiddenBage{
    [self.arrowImageView clearBadge];
}

#pragma mark - getter & setter
- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.layer.cornerRadius = 10;
        _containerView.clipsToBounds = YES;
        _containerView.backgroundColor = [UIColor whiteColor];
    }
    return _containerView;
}

- (UIImageView *)arrowImageView {
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right"]];
    }
    return _arrowImageView;
}
- (UIImageView *)headerImageView {
    if (!_headerImageView) {
        _headerImageView = [UIImageView new];
    }
    return _headerImageView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel labelWithFont:[HKFontHelper appcnBoldFontWithSize:16.f]
                                    bgColor:nil
                                  textColor:UIColorFromRGB(0x333333) textAligment:NSTextAlignmentLeft];
    }
    return _titleLab;
}

- (UILabel *)subTitleLab {
    if (!_subTitleLab) {
        _subTitleLab = [UILabel labelWithFont:[HKFontHelper appcnFontWithSize:13.f]
                                    bgColor:nil
                                  textColor:UIColorFromRGB(0x828282) textAligment:NSTextAlignmentLeft];
    }
    return _subTitleLab;
}
@end
